package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.INavDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.util.JDBCUtils;
import redis.clients.jedis.Jedis;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NavDaoImpl implements INavDao {
    private void clearNavRedis(){
        Jedis jedis = new Jedis();
        jedis.del("nav_id");
        jedis.del("nav_title");
        jedis.del("nav_href");
//        jedis.del("nav_create_time");
//        jedis.del("nav_update_time");
//        jedis.del("nav_enable");
//        jedis.del("nav_note");
    }

    private NavModel getResult(ResultSet rs) throws SQLException {
        NavModel navModel = new NavModel();
        navModel.setId(rs.getInt("id"));
        navModel.setTitle(rs.getString("title"));
        navModel.setHref(rs.getString("href"));
        navModel.setCreateTime(rs.getString("create_time"));
        navModel.setUpdateTime(rs.getString("update_time"));
        navModel.setEnabled(rs.getInt("enable"));
        navModel.setNote(rs.getString("note"));
        return navModel;
    }
    private NavModel getNavResult(String sql) {
        ResultSet rs = JDBCUtils.getResultSet(sql);
        NavModel navModel = null;
        try {
            while (rs.next()) {
                navModel = getResult(rs);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return navModel;
    }
    private List<NavModel> getNavListResult(String sql){
        ResultSet rs = JDBCUtils.getResultSet(sql);
        List<NavModel> list = new ArrayList<>();
        NavModel navModel = null;
        try {
            while (rs.next()) {
                navModel = getResult(rs);
                list.add(navModel);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    @Override
    public int addNav(NavModel navModel) {
        String sql = "insert into nav (title,href,create_time,enable) values('" + navModel.getTitle() + "','" + navModel.getHref() + "','" + navModel.getCreateTime() + "',0)";
        clearNavRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int deleteNav(int id) {
        String sql = "delete from nav where id=" + id;
        clearNavRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int editNav(NavModel navModel) {
        String sql = "update nav set title='" + navModel.getTitle() + "',href='"
                + navModel.getHref() + "',update_time='" + navModel.getUpdateTime() +
                "' where id=" + navModel.getId();
        clearNavRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<NavModel> getNavList() {
        String sql = "select * from nav";
        return getNavListResult(sql);
    }

    @Override
    public NavModel getNav(int id) {
        String sql = "select * from nav where id=" + id;
        return getNavResult(sql);
    }

    @Override
    public List<NavModel> getNavList(int page, int size) {
        String sql = "select * from nav limit " + (page - 1) * size + "," + size;
        return getNavListResult(sql);
    }

    @Override
    public List<NavModel> getNavList(int page, int size, String sort) {
        String sql = "select * from nav order by " + sort + " limit " + (page - 1) * size + "," + size;
        return getNavListResult(sql);
    }

    @Override
    public List<NavModel> searchNav(String sql) {
        return getNavListResult(sql);
    }

    @Override
    public long getNavCount() {
        String sql = "select count(*) from nav";
        return JDBCUtils.getCount(sql);
    }

    @Override
    public int updateEdit(int id, int enable) {
        String sql = "update nav set enable=" + enable + " where id=" + id;
        clearNavRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<NavModel> getNavListByRedis() {
        Jedis jedis = new Jedis();
        List<NavModel> list = new ArrayList<>();
        List<String> navId = jedis.lrange("nav_id", 0, -1);
        for (int i = 0; i < navId.size(); i++) {
            NavModel navModel = new NavModel();
            navModel.setId(Integer.valueOf(navId.get(i)));
            navModel.setTitle(jedis.lindex("nav_title", i));
            navModel.setHref(jedis.lindex("nav_href", i));
//            navModel.setCreateTime(jedis.lindex("nav_create_time",i));
//            navModel.setUpdateTime(jedis.lindex("nav_update_time",i));
//            navModel.setEnabled(Integer.valueOf(jedis.lindex("nav_enable",i)));
//            navModel.setNote(jedis.lindex("nav_note",i));
            list.add(navModel);
        }
        return list;
    }

    @Override
    public void setNavListInRedis(List<NavModel> navList) {
        Jedis jedis = new Jedis();
        for (NavModel nav : navList) {
            jedis.rpush("nav_id", String.valueOf(nav.getId()));
            jedis.rpush("nav_title", nav.getTitle());
            jedis.rpush("nav_href", nav.getHref());
//            jedis.rpush("nav_create_time",nav.getCreateTime());
//            jedis.rpush("nav_update_time",nav.getUpdateTime());
//            jedis.rpush("nav_enable", String.valueOf(nav.getEnabled()));
//            jedis.rpush("nav_note",nav.getNote());
        }
    }
}
